package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pub.MessageConstant;
import com.itheima.service.CheckgroupService;
import com.itheima.service.CheckitemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("checkgroup")
public class CheckgroupController {

    @Reference
    CheckgroupService checkgroupService;

    // 分页查询检查组
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map page = checkgroupService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
//
    // 添加或者更新检查组
    @RequestMapping("addGroup")
    public Result addGroup(@RequestBody CheckGroup checkGroup,@RequestParam("checkitemIds") Integer[] checkitemIds){
        try {
            // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新
            checkgroupService.addGroup(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.UPDATE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("findCheckitemIds")
    public Result findCheckitemIds(@RequestParam("groupId") Integer groupId){
        try {
            // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新
            List<Integer> checkitemIds = checkgroupService.findCheckitemIds(groupId);
            return new Result(true, "",checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"");
        }
    }

//
    // 根据id值删除检查项
    @RequestMapping("deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(@RequestParam("id") Integer id){
        try {
            CheckGroup checkGroup = new CheckGroup();
            checkGroup.setId(id);
            checkGroup.setIs_delete(1);
            // 目前来看此种方法最适合进行逻辑删除  值为null的不更新,  那为0或者0.0呢??(price变量 属性为float 为什么不更新)
            checkgroupService.updateById(checkGroup);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
//
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<CheckGroup> list = checkgroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
